﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Web;
using ComLib.Cryptography;


namespace ComLib.Web.Lib.Services.GravatarSupport
{
    /// <summary>
    /// Twitter class for getting tweets
    /// </summary>
    public class Gravatar
    {
        private string URL = "http://www.gravatar.com/avatar/{0}{1}?d={2}&s={3}&r={4}";


        public Gravatar()
        {
            Size = 80;
            Rating = "g";
            IconType = "";
        }


        /// <summary>
        /// Gets or sets the email.
        /// </summary>
        /// <value>The email.</value>
        public string Email { get; set; }


        /// <summary>
        /// extension used for image. e.g. jpg or png if applicable.
        /// </summary>
        public string ImageExtension { get; set; }


        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        /// <value>The size.</value>
        public int Size { get; set; }


        /// <summary>
        /// Rating can either be g, pg, r, x
        /// </summary>
        public string Rating { get; set; }


        /// <summary>
        /// Gets or sets the type of the icon.
        /// </summary>
        /// <value>The type of the icon.</value>
        public string IconType { get; set; }


        /// <summary>
        /// Gets the URL.
        /// </summary>
        /// <value>The URL.</value>
        public string Url
        {
            get
            {
                if (Size <= 0)
                    Size = 80;

                if (string.IsNullOrEmpty(Rating))
                    Rating = "g";

                // 1. Hash the email
                string emailHash = Crypto.ToMD5Hash(Email.ToLower());
                emailHash = emailHash.ToLower();
                // 2. Format the url
                string url = string.Format(URL, emailHash, ImageExtension, IconType, Size, Rating);
                return url;
            }
        }
    }
}
